const fs = require('fs');
const path = require('path');
const dayjs = require('dayjs')

// console.log(dayjs().format('YYYY-MM-DD'));
const pathFile = path.join(__dirname, './data/info.json')

// 新闻接口
function news() {
    const pathFile = path.join(__dirname, './data/news.json')
    const data = fs.readFileSync(pathFile, 'utf-8');
    const arr = JSON.parse(data)
    arr.forEach(el => {
        el.publish_time = dayjs(el.publish_time).format('YYYY-MM-DD HH:mm:ss')
    });
    return arr
}

// 获取学生接口
function students() {

    const data = fs.readFileSync(pathFile, 'utf-8');
    return JSON.parse(data)
}


// 幸运儿
function lucystar() {

    const data = fs.readFileSync(pathFile, 'utf-8');
    const arr = JSON.parse(data)
    const random = parseInt(Math.random() * arr.length)
    // console.log(random);
    // console.log("抽到了：" + arr[random].name);
    arr[random].count++
    fs.writeFileSync(pathFile, JSON.stringify(arr))
    return arr[random]

}

module.exports = {
    students,
    lucystar,
    news
}